﻿using Aspose.Cells;
using LiveCharts;
using LiveCharts.Wpf;
using Microsoft.WindowsAPICodePack.Dialogs;
using NPOI.XSSF.UserModel;
using Stylet;
using SUT.PrintEngine.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using WeighingRecord.Models;
using WeighingRecord.Services;
using WeighingRecord.Views;

namespace WeighingRecord.ViewModels
{
    public class PzReportViewModel : Screen
    {
        private IWindowManager windowManager;

        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }

        public Brush WeekBorderBrush { get; set; }
        public Brush WeekBackground { get; set; }
        public Brush MonthBorderBrush { get; set; }
        public Brush MonthBackground { get; set; }
        public Brush YearBorderBrush { get; set; }
        public Brush YearBackground { get; set; }

        public string ProvicerTitle
        {
            get
            {
                return WeighingRecord.Globalspace.ProvicerTitle;
            }
        }

        public PzReportViewModel(IWindowManager windowManager, List<WeighingRecordModel> list)
        {
            this.windowManager = windowManager;
            Getdata(list);
        }

        public PzReportViewModel(IWindowManager windowManager, WeighingRecordModel record)
        {
            this.windowManager = windowManager;
            Getdata(record);
        }

        public void Getdata(WeighingRecordModel record)
        {
            BrushConverter conv = new BrushConverter();
            Brush FEF03E = conv.ConvertFromInvariantString("#FEF03E") as Brush;
            Brush F5F7F9 = conv.ConvertFromInvariantString("#F5F7F9") as Brush;
            Brush E1E6EC = conv.ConvertFromInvariantString("#E1E6EC") as Brush;

            WeekBorderBrush = E1E6EC;
            WeekBackground = F5F7F9;
            MonthBorderBrush = E1E6EC;
            MonthBackground = F5F7F9;
            YearBorderBrush = E1E6EC;
            YearBackground = F5F7F9;

            List<WeighingRecordModel> list = SQLDataAccess.GetWeighingRecords(record.Ch).OrderBy(p => p.Bh).ToList();

            SeriesCollection = new SeriesCollection();

            if (list == null || list.Count <= 0) return;
            int count = 0;

            var endDate = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59"));

            YearBorderBrush = FEF03E;
            YearBackground = FEF03E;

            SeriesCollection.Add(new LineSeries
            {
                Title = $"{record.Ch}-历史记录",
                Values = new ChartValues<double>()
            });
            Labels = new string[list.Count];
            foreach (var date in list)
            {
                Labels[count] = $"{date.Pzrq}";
                var info = SeriesCollection.FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(date.Pz))
                {
                    info.Values.Add((double.Parse(date.Pz)));
                }
                else
                {
                    info.Values.Add(0);
                }
               
                count++;
            }
            //Formatter = value => value.ToString() + "T";
            Formatter = value => value.ToString();
        }

        public void Getdata(List<WeighingRecordModel> list)
        {
            BrushConverter conv = new BrushConverter();
            Brush FEF03E = conv.ConvertFromInvariantString("#FEF03E") as Brush;
            Brush F5F7F9 = conv.ConvertFromInvariantString("#F5F7F9") as Brush;
            Brush E1E6EC = conv.ConvertFromInvariantString("#E1E6EC") as Brush;

            WeekBorderBrush = E1E6EC;
            WeekBackground = F5F7F9;
            MonthBorderBrush = E1E6EC;
            MonthBackground = F5F7F9;
            YearBorderBrush = E1E6EC;
            YearBackground = F5F7F9;

            SeriesCollection = new SeriesCollection();

            if (list == null || list.Count <= 0) return;
            int count = 0;

            var endDate = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59"));

            YearBorderBrush = FEF03E;
            YearBackground = FEF03E;

            var dateGroup = list.GroupBy(g => (new { DateTime.Parse(g.Jzrq).Year, DateTime.Parse(g.Jzrq).Month })).ToList();


            SeriesCollection.Add(new LineSeries
            {
                Title = "整体称重趋势",
                Values = new ChartValues<double>()
            });
            Labels = new string[dateGroup.Count];
            foreach (var date in dateGroup)
            {
                Labels[count] = $"{(date.Key.Year.ToString())}-{(date.Key.Month.ToString())}";

                var sumValue = date.Sum(s => double.Parse(s.Jz));
                var info = SeriesCollection.FirstOrDefault();
                info.Values.Add(sumValue);
                count++;
            }
            //Formatter = value => value.ToString() + "T";
            Formatter = value => value.ToString();

            ////modifying the series collection will animate and update the chart
            //SeriesCollection.Add(new LineSeries
            //{
            //    Title = "Series 4",
            //    Values = new ChartValues<double> { 5, 3, 2, 4 },
            //    LineSmoothness = 0, //0: straight lines, 1: really smooth lines
            //    PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
            //    PointGeometrySize = 50,
            //    PointForeground = Brushes.Gray
            //});

            //modifying any series values will also animate and update the chart
            //SeriesCollection[3].Values.Add(5d);


        }

    }
}